﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms.Integration;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Vlc.DotNet.Forms;

namespace VideoWallpaper
{
    /// <summary>
    /// DeskWindow.xaml 的交互逻辑
    /// </summary>
    public partial class DeskWindow : Window
    {
        public DeskWindow()
        {
            InitializeComponent();
        }

        List<VlcControl> vlcControls = new List<VlcControl>();
        List<WindowsFormsHost> windowsForms = new List<WindowsFormsHost>();


        // vlc参数
        public string[] options = new string[] {
            "--input-repeat=65535"
        };


        // 添加组件
        public void AddDisplayComponent(int offset, int width, int height)
        {
            DockPanel dockPanel = new DockPanel();
            dockPanel.Margin = new Thickness(offset, 0, 0, 0);
            // 左对齐
            dockPanel.HorizontalAlignment = HorizontalAlignment.Left;
            dockPanel.Width = width;
            dockPanel.Height = height;
            // 不显示，不留空间
            dockPanel.Visibility = Visibility.Collapsed;
            // wpf托管windows窗体
            WindowsFormsHost windowsFormsHost = new WindowsFormsHost();
            // 初始化vlc
            VlcControl vlcControl = new VlcControl();
            DirectoryInfo directory = new DirectoryInfo(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "libvlc", IntPtr.Size == 4 ? "win-x86" : "win-x64"));
            // 开始初始化
            vlcControl.BeginInit();
            // 指定库位置
            vlcControl.VlcLibDirectory = directory;
            vlcControl.VlcMediaplayerOptions = options;
            vlcControl.EndInit();

            // 添加组件
            windowsFormsHost.Child = vlcControl;
            dockPanel.Children.Add(windowsFormsHost);
            rootGrid.Children.Add(dockPanel);
            // 添加到列表
            vlcControls.Add(vlcControl);
            windowsForms.Add(windowsFormsHost);
        }


        // 播放
        public void PlayVideo(string uri)
        {
            IEnumerable<DockPanel> dockPanels = rootGrid.Children.OfType<DockPanel>();
            // 显示dockpanel
            dockPanels.ToList().ForEach(dock => dock.Visibility = Visibility.Visible);
            // vlc播放视频
            foreach (var vlc in vlcControls)
            {
                vlc.Audio.Volume = 0;
                vlc.Play(new Uri(uri));
                //Thread thread = new Thread(() => {
                //});
                //thread.Start();
            }
        }


        // 关闭窗口
        private void Window_Closed(object sender, EventArgs e)
        {
            foreach (var vlc in vlcControls)
            {
                vlc.Dispose();
            }
            foreach (var host in windowsForms)
            {
                host.Dispose();
            }
        }
    }
}
